<?php

namespace App\Http\Controllers\Home;

use App\Model\Department;
use App\Model\No_store_station;
use App\Model\Station;
use App\Model\Store;
use App\Model\Store_station;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
//店铺配置
class StoreController extends CommonController
{

	protected $model;
	protected $store_station;
	protected $Department;
	protected $station;
	protected $No_store_station;
	public function __construct()
	{
		parent::__construct();

		$this->model=new Store();
		$this->store_station=new Store_station();
		$this->No_store_station=new No_store_station();
		$this->Department=new Department();
		$this->station=new Station();

}
//	店铺列表
	public function listStore(Request $request){
		$where['company_id']=$this->company_id;
		try{
			$where['name']=isset($request->name)?$request->name:null;
			$where['owner_name']=isset($request->owner_name)?$request->owner_name:null;
			$where['address']=isset($request->address)?$request->address:null;
		    $where['status']=isset($request->status)?$request->status:null;
			$where['start_time']=isset($request->start_time)?$request->start_time:null;
			$where['end_time']=isset($request->end_time)?$request->end_time:null;

			$data=$this->model->lists($where);
		}catch (Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}

	}

//	新增店铺
	public function addStore(Request $request){
		$this->validate($request,[
				'department_id' => 'required|unique:store|Integer',
				'people_num' => 'required|Integer',
				'month_hour' => 'required|numeric',
				'continue_hour' => 'required|numeric',
				'radius' => 'required|numeric',
				'longitude' => 'required|numeric',
				'latitude' => 'required|numeric',
				'address' => 'required|String',
			]);
		DB::beginTransaction();
		try{
			$departmentData=$this->department_name($request->department_id);
			$data=$request->only($this->model->getFillable());
			$data['name']=$departmentData->name;
			$data['leader_id']=$departmentData->leader_id;
			$data['company_id']=$this->company_id;
			$result=$this->model->create($data);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}

//  编辑店铺
	public function editStore(Request $request){

		try{
			$result=$this->model->where(['id'=>$request->id])->first();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
//			return $message;
			return $this->faild($message);
		}
	}


//	编辑保存店铺
	public function saveStore(Request $request){
		$this->validate($request,[
//			'department_id' => 'required|unique:store|Integer',
			'people_num' => 'required|Integer',
			'month_hour' => 'required|numeric',
			'continue_hour' => 'required|numeric',
			'radius' => 'required|numeric',
			'longitude' => 'required|numeric',
			'latitude' => 'required|numeric',
			'address' => 'required|String',
			'id'=> 'required|Integer',
		]);
		DB::beginTransaction();
		try{
			$result=$this->model->where(['id'=>$request->id])->update($request->input());
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}

//	店铺岗位列表
	public function listStore_station(Request $request){
		$data=$this->store_station->allStations_($request->store_id);
		return $this->success($data);
	}
//	店铺岗位列表接口
	public function listStore_station_(Request $request){
		$department_id=$request->department_id;
		$data=$this->store_station->allStations__($request->department_id);
////		获取所有的店铺岗位
//		$store_station=$this->store_station->where(['status'=>1])
//			->select('id','department_id','name')->get()->toArray();
////		获取所有的非门店店铺岗位
//		$no_store_station=$this->No_store_station
//			->select('id','department_id','name')->get()->toArray();
//		$array=array_merge($store_station,$no_store_station);
//		$newArr=[];$Arr=[];
//		foreach($array as $kk=>$vv){
//			$newArr[$vv['department_id']][]=$vv;
//		}
//		if(isset($newArr[$department_id])){
//			$Arr=$newArr[$department_id];
//		}
		return $this->success($data);
	}
//	新增店铺岗位
	public function addStore_station(Request $request){
			$this->validate($request,[
				'station_id' => 'required|Integer',
				'store_id'=>'required|Integer',
				'name'=>'required|String',
				'num'=>'required|Integer',
				'standard_hour' => 'required|numeric',
				'status' => 'required|numeric',
				'start_time' => 'required|Date',
				'end_time' => 'required|Date',
				'comment' => 'required|string',
			]);
		DB::beginTransaction();
		try{
			$data=$request->only($this->store_station->getFillable());
			$data['department_id']=$this->model->where(['id'=>$request->store_id])->first()->department_id;
			$data['company_id']=$this->company_id;
			$data['name']=$this->station_name($request->store_id);
			$result=$this->store_station->create($data);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}

//  编辑店铺岗位
	public function editStore_station(Request $request){
		try{
			$result=$this->store_station->store_station_desc($request->id);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}
//	编辑保存店铺岗位
	public function save_store_station(Request $request){

		$this->validate($request,[
			'id'=>'required|Integer',
			'station_id' => 'required|Integer',
			'store_id'=>'required|Integer',
			'num'=>'required|Integer',
			'standard_hour' => 'required|numeric',
			'status' => 'required|numeric',
			'start_time' => 'required|Date',
			'end_time' => 'required|Date',
			'comment' => 'required|string',
		]);
		DB::beginTransaction();
		try{
			$data=$request->only($this->store_station->getFillable());
			$data['name']=$this->station_name($request->station_id);
			$result=$this->store_station->where(['id'=>$request->id])->update($data);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}

//	查询部门名称
	public function department_name($department_id){
		return $this->Department->where(['id'=>$department_id])->first();
	}
//	查询岗位名称
	public function station_name($station_id){
		return $this->station->where(['id'=>$station_id])->first()->name;
	}
}
